﻿package com.saint {
	
	import adobe.utils.CustomActions;
	import caurina.transitions.Tweener;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.Shader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.ShaderFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import com.video.SimpleVideo;
	import flash.utils.ByteArray;
	
	[SWF(width = "665", height = "315", frameRate = "30", backgroundColor = "#000000")]
	
	public class Application extends Sprite {
		
		[Embed("../../GreyScale.pbj", mimeType = "application/octet-stream")]
		private var pixelBenderFilter:Class;
		
		private var shader:				Shader;
		private var filter:				ShaderFilter;
		private var vid:				SimpleVideo;
		private var vidBitmapData:		BitmapData;
		private var output:				Bitmap;
		private var displayCam:			Boolean;
		private var displayOutput:		Boolean;
		
		private var grid:				PixelGrid;
		private var gridType:			int = 0;
		private var gridTypes:			int = 6;
		
		private var cameraW:			int = 640;//460;
		private var cameraH:			int = 287;//206;
		
		public function Application() {
			
			displayCam		=	false;
			displayOutput	=	false;
			
			setupVideo();
		}
		
		private function setupVideo():void {
			vid = new SimpleVideo(cameraW, cameraH);
			vid.x = vid.y = 10;
			vid.playVideo("video/DecodeIdent.flv");
			vid.addEventListener("ready", onReady);
		}
		
		private function onReady(e:Event):void {
			init();
		}
		
		private function init() {
			setupVideoFilter();
			
			if (displayCam) {
				addChild(vid);
			}
			if (displayOutput) {
				addChild(output);
			}
			
			buildInterface();
			buildGrid();
			
			addEventListener(Event.ENTER_FRAME, doEF, false, 0, true);
		}
		
		private function setupVideoFilter():void {
			vidBitmapData = new BitmapData(cameraW, cameraH, false, 0 );
			
			shader = new Shader(new pixelBenderFilter() as ByteArray);
			filter = new ShaderFilter(shader);
			
			output = new Bitmap(vidBitmapData);
			output.filters = [filter];
		}
		
		private function buildGrid():void {
			if (grid != null) {
				removeGrid();
			}
			grid = new PixelGrid(cameraW, cameraH, gridType);
			grid.x = grid.y = 10;
			gridType = gridType == gridTypes ? 0 : gridType + 1;
			
			addChildAt(grid, this.numChildren - 2);
			//addChild(grid);
		}
		
		private function removeGrid():void {
			removeChild(grid);
		}
		
		private function buildInterface():void {
			var toggle:SimpleGraphicBttn = new SimpleGraphicBttn(cameraW / 2 - 10, 20);
			var next:SimpleGraphicBttn = new SimpleGraphicBttn(cameraW / 2 - 10, 20);
			
			toggle.x = 5;
			next.x = toggle.x + toggle.width + 5;
			
			toggle.y = next.y = cameraH + 25;
			
			toggle.addEventListener(UpdateEvent.TYPE, onToggle, false, 0, true);
			next.addEventListener(UpdateEvent.TYPE, onNext, false, 0, true);
			
			addChild(toggle);
			addChild(next);
		}
		
		private function onToggle(e:Event):void {
			grid.toggleInvert();
		}
		private function onNext(e:Event):void {
			buildGrid();
		}
		
		private function doEF(e:Event):void {
			vidBitmapData.draw(vid);
			grid.update(vidBitmapData);
		}
	}
	
}